// Globals
tiles = new Array;

window.onload = function() {
	getData();
	loadfill();
	window.setInterval("flipper();", 2500);
};

function getData() {
	var i = 0;
	while (i < 8) {
		var tf0 = "#tileTitle";
		var tf = tf0 + (i + 1);
		
		var td0 = "#tileDescription";
		var td = td0 + (i + 1);

		var tp0 = "#tilePrice";
		var tp = tp0 + (i + 1);

		var pic0 = "#tilePic";
		var pic = pic0 + (i + 1);

		tiles[i] = new Object({
			title : $(tf).html(), // status: 1
			description : $(td).html(), // status 2
			price : $(tp).html(), // status 3
			pic : $(pic).html(), // status: 4
			shows : 1
		});

		i++;
	}
}

function flipper() {
	var ftile = (rand(1, 8)) - 1;
	var show = rand(1, 4);

	flip(ftile, show);
}

function loadfill() {
	var i = 0;
	while (i < 8) {
		var tile0 = ".tile";
		var tile = tile0 + (i + 1);

		$(tiles[i].title).appendTo(tile);

		i++;
	}
}

function flip(n, display) {
	// Zustandswiederholungen vermeiden
	
	if(display == tiles[n].shows) {
		if(tiles[n].shows == 4) {
			display = 1;
		}
		else {
			display = display + 1;
		}
	}

	// shows Variable setzten
	tiles[n].shows = display;

	switch (display) {
	case 1:
		display = tiles[n].title;
		break;
	case 2:
		display = tiles[n].description;
		break;
	case 3:
		display = tiles[n].price;
		break;
	case 4:
		display = tiles[n].pic;
		break;
	}

	switch (n) {
	case 0:
		$(".tile1").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 1:
		$(".tile2").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 2:
		$(".tile3").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 3:
		$(".tile4").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 4:
		$(".tile5").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 5:
		$(".tile6").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 6:
		$(".tile7").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;

	case 7:
		$(".tile8").flip({
			direction : 'tb',
			dontChangeColor : false,
			bgColor : '#75BA44',
			color : '#75BA44',
			speed : 1000,
			content : display
		})
		break;
	}
}

function sleep(delay) {
	var start = new Date().getTime();
	while (new Date().getTime() < start + delay)
		;
}

function rand(low, high) {

	var numLow = low;
	var numHigh = high;

	var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;

	return Math.floor(Math.random() * adjustedHigh) + parseFloat(numLow);
}

function isNumeric(n) {
	return !isNaN(n);
}